package com.wxl.common.wiget

import android.content.Context
import android.view.KeyEvent
import com.lxj.xpopup.impl.ConfirmPopupView
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener

class CancelPop : ConfirmPopupView {

    constructor(context: Context) : super(context)

    private lateinit var  confirmListener: OnConfirmListener
    private lateinit var cancelListener: OnCancelListener


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            dismiss()
            cancelListener.onCancel()
        }
        if(keyCode == KeyEvent.KEYCODE_ENTER){
            dismiss()
            confirmListener.onConfirm()
        }
        return true
    }

    /****************************************** 便捷方法 ****************************************/
    /****************************************** 便捷方法  */
    /**
     * 显示确认和取消对话框
     *
     * @param title           对话框标题，传空串会隐藏标题
     * @param content         对话框内容
     * @param cancelBtnText   取消按钮的文字内容
     * @param confirmBtnText  确认按钮的文字内容
     * @param confirmListener 点击确认的监听器
     * @param cancelListener  点击取消的监听器
     * @param isHideCancel    是否隐藏取消按钮
     * @return
     */
    companion object {
        fun asConfirm(
            context: Context,
            title: CharSequence,
            content: CharSequence,
            confirmListener: OnConfirmListener,
            cancelListener: OnCancelListener
        ): ConfirmPopupView {
            val popupView = CancelPop(context)
            popupView.setTitleContent(title, content, null)
            popupView.confirmListener = confirmListener
            popupView.cancelListener = cancelListener
            return popupView
        }
    }
}